﻿using System;

using ApplicationLibrary;
using ApplicationLibrary.SqlConnector;

namespace Plumber.Masters.ModuleData
{
    internal class RegisterModule : ApplicationLibrary.SqlConnector.RegisterModule
    {
        public override string Application()
        {
            return "PLMB";
        }

        public override string Module()
        {
            return "MAST";
        }

        public override int CurrentVersion()
        {
            return 6;
        }

        public override Version DllVersion
        {
            get
            {
                return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            }
        }

        public override void RegisterCountersAndCodes()
        {
            RRMasterTypeForMaster rrMasterType = new RRMasterTypeForMaster();
            rrMasterType.Find();

            for (int t = 0; t < rrMasterType.Count; t++)
                GlobalInfo.AddCodeType(rrMasterType.GetValue<string>(PL_MasterType.Code, t), rrMasterType.GetValue<string>(PL_MasterType.Description, t));
        }

        public override void AddScrounger(ApplicationLibrary.DBManager.iScrounger frm, NameSpace nameSpace)
        {
            //if (nameSpace.ApplicationPath == "Plumber.Plumber.Masters.Forms.Masters")
            //    frm.ScroungerList.Add()
        }

        public override ApplicationLibrary.Preferences.PreferencePanel[] RegisterPreferences()
        {
            //return new ApplicationLibrary.Preferences.PreferencePanel[]
            //{
            //    new AccountingPreferencePanel()
            //};
            return null;
        }

        protected override bool CreateDBTables()
        {
            SqlCreateTable newTable = new SqlCreateTable(SqlABConnection);

            #region PL_Masters

            if (SearchTable(PL_Masters.Name))
            {
                newTable.NewTable(PL_Masters.Name);
                newTable.AddColumn(PL_Masters.IsAccountingType);
                newTable.AddColumn(PL_Masters.Type);
                newTable.AddColumn(PL_Masters.Code);
                newTable.AddColumn(PL_Masters.Description);
                newTable.AddColumn(PL_Masters.Address);
                newTable.AddColumn(PL_Masters.ZipCode);
                newTable.AddColumn(PL_Masters.City);
                newTable.AddColumn(PL_Masters.County);
                newTable.AddColumn(PL_Masters.Phone1);
                newTable.AddColumn(PL_Masters.Phone2);
                newTable.AddColumn(PL_Masters.Fax);
                newTable.AddColumn(PL_Masters.Refer1);
                newTable.AddColumn(PL_Masters.Refer2);
                newTable.AddColumn(PL_Masters.Title);
                newTable.AddColumn(PL_Masters.Email);

                newTable.AddColumn(PL_Masters.TaxNo);
                newTable.AddColumn(PL_Masters.FiscalCode);
                newTable.AddColumn(PL_Masters.Administrator);

                newTable.AddColumn(PL_Masters.Disabled);
                newTable.AddColumn(PL_Masters.Blocked);

                newTable.AddColumn(PL_Masters.TaxCode);
                newTable.AddColumn(PL_Masters.HasRestraint);
                newTable.AddColumn(PL_Masters.RestraintCode);

                newTable.AddPrimaryKey(PL_Masters.Type, PL_Masters.Code);

                newTable.Create();
            }

            #endregion

            #region PL_Titles

            if (SearchTable(PL_Titles.Name))
            {
                newTable.NewTable(PL_Titles.Name);

                newTable.AddColumn(PL_Titles.Title);
                newTable.AddColumn(PL_Titles.Description);

                newTable.AddPrimaryKey(PL_Titles.Title);

                newTable.Create();
            }

            #endregion

            #region PL_MasterType

            if (SearchTable(PL_MasterType.Name))
            {
                newTable.NewTable(PL_MasterType.Name);

                newTable.AddColumn(PL_MasterType.IsAccountingType);
                newTable.AddColumn(PL_MasterType.Code);
                newTable.AddColumn(PL_MasterType.Description);
                newTable.AddColumn(PL_MasterType.CodeMode);
                newTable.AddColumn(PL_MasterType.MasterType);
                newTable.AddColumn(PL_MasterType.AccountType);
                newTable.AddColumn(PL_MasterType.CanReceiveInvoice);

                newTable.AddColumn(PL_MasterType.TaxCode);
                newTable.AddColumn(PL_MasterType.HasRestraint);
                newTable.AddColumn(PL_MasterType.RestraintCode);
                newTable.AddColumn(PL_MasterType.IsPrefer);
                newTable.AddColumn(PL_MasterType.CopyDocNo);

                newTable.AddPrimaryKey(PL_MasterType.Code);

                newTable.Create();
            }

            #endregion

            #region PL_TaxCode

            if (SearchTable(PL_TaxCode.Name))
            {
                newTable.NewTable(PL_TaxCode.Name);

                newTable.AddColumn(PL_TaxCode.Code);
                newTable.AddColumn(PL_TaxCode.Description);
                newTable.AddColumn(PL_TaxCode.Percent);

                newTable.AddPrimaryKey(PL_TaxCode.Code);

                newTable.Create();
            }

            #endregion

            #region PL_Descriptions

            if (SearchTable(PL_Descriptions.Name))
            {
                newTable.NewTable(PL_Descriptions.Name);

                newTable.AddColumn(PL_Descriptions.Type);
                newTable.AddColumn(PL_Descriptions.Code);
                newTable.AddColumn(PL_Descriptions.Description);

                newTable.AddPrimaryKey(new iColumn[] { PL_Descriptions.Type, PL_Descriptions.Code });

                newTable.Create();
            }

            #endregion

            #region PL_Company

            if (SearchTable(PL_Company.Name))
            {
                newTable.NewTable(PL_Company.Name);
                newTable.AddColumn(PL_Company.Code);
                newTable.AddColumn(PL_Company.Description);
                newTable.AddColumn(PL_Company.Address);
                newTable.AddColumn(PL_Company.ZipCode);
                newTable.AddColumn(PL_Company.City);
                newTable.AddColumn(PL_Company.County);
                newTable.AddColumn(PL_Company.Phone1);
                newTable.AddColumn(PL_Company.Phone2);
                newTable.AddColumn(PL_Company.Fax);
                newTable.AddColumn(PL_Company.Refer1);
                newTable.AddColumn(PL_Company.Refer2);
                newTable.AddColumn(PL_Company.Title);
                newTable.AddColumn(PL_Company.Email);
                newTable.AddColumn(PL_Company.TaxNo);
                newTable.AddColumn(PL_Company.FiscalCode);

                newTable.AddPrimaryKey(PL_Company.Code);

                newTable.Create();
            }

            #endregion

            return true;
        }

        protected override bool UpdateDBTables()
        {
            if (dbVersion < 2)
            {
                SqlCreateTable alterMaster = new SqlCreateTable(SqlABConnection);

                if (!SearchColumn(PL_Masters.Email))
                {
                    alterMaster.AlterTable(PL_Masters.Name);
                    alterMaster.AddColumn(PL_Masters.Email);
                    alterMaster.Alter();
                }
            }
            if (dbVersion < 3)
            {
                SqlCreateTable alterMaster = new SqlCreateTable(SqlABConnection);

                if (!SearchColumn(PL_MasterType.IsAccountingType))
                {
                    alterMaster.AlterTable(PL_MasterType.Name);
                    alterMaster.AddColumn(PL_MasterType.IsAccountingType);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_MasterType.Name);
                    qb.AddSet<bool>(PL_MasterType.IsAccountingType, false);
                    qb.AddCompareIsNull(PL_MasterType.IsAccountingType);
                    int nRow = qb.ExecuteNonQuery();
                }

                if (!SearchColumn(PL_MasterType.DocNoRequired))
                {
                    alterMaster.AlterTable(PL_MasterType.Name);
                    alterMaster.AddColumn(PL_MasterType.DocNoRequired);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_MasterType.Name);
                    qb.AddSet<bool>(PL_MasterType.DocNoRequired, false);
                    qb.AddCompareIsNull(PL_MasterType.DocNoRequired);
                    int nRow = qb.ExecuteNonQuery();
                }
            }
            if (dbVersion < 4)
            {
                SqlCreateTable alterMaster = new SqlCreateTable(SqlABConnection);

                if (!SearchColumn(PL_Masters.IsAccountingType))
                {
                    alterMaster.AlterTable(PL_Masters.Name);
                    alterMaster.AddColumn(PL_Masters.IsAccountingType);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_Masters.Name);
                    qb.AddSet<bool>(PL_Masters.IsAccountingType, false);
                    qb.AddCompareIsNull(PL_Masters.IsAccountingType);
                    int nRow = qb.ExecuteNonQuery();
                }
            }

            if (dbVersion < 5)
            {
                SqlCreateTable alterMaster = new SqlCreateTable(SqlABConnection);

                if (!SearchColumn(PL_MasterType.IsPrefer))
                {
                    alterMaster.AlterTable(PL_MasterType.Name);
                    alterMaster.AddColumn(PL_MasterType.IsPrefer);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_MasterType.Name);
                    qb.AddSet<bool>(PL_MasterType.IsPrefer, false);
                    qb.AddCompareIsNull(PL_MasterType.IsPrefer);

                    qb.ExecuteNonQuery();
                }
            }

            if (dbVersion < 6)
            {
                SqlCreateTable alterMaster = new SqlCreateTable(SqlABConnection);

                if (!SearchColumn(PL_MasterType.CopyDocNo))
                {
                    alterMaster.AlterTable(PL_MasterType.Name);
                    alterMaster.AddColumn(PL_MasterType.CopyDocNo);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_MasterType.Name);
                    qb.AddSetFromColumn(PL_MasterType.CopyDocNo, PL_MasterType.DocNoRequired);
                    qb.AddCompareIsNull(PL_MasterType.CopyDocNo);

                    qb.ExecuteNonQuery();
                }
            }
            return true;
        }
    }
}